﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Deployment.Application;

namespace ShredTheLog.Common
{
    public sealed class VersionInfo
    {
        public string Product
        {
            get { return GetEntryAssemblyAttribute<AssemblyProductAttribute>().Product; }
        }

        public string Copyright
        {
            get { return GetEntryAssemblyAttribute<AssemblyCopyrightAttribute>().Copyright; }
        }

        public string Version
        {
            get 
            {
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    var version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
                    return version.ToString();
                }
                else
                {
                    return Assembly.GetEntryAssembly().GetName().Version.ToString(); 
                } 
            }
        }

        private T GetEntryAssemblyAttribute<T>() where T : Attribute
        {
            var entryAssembly = Assembly.GetEntryAssembly();
            var result = (T)Attribute.GetCustomAttribute(entryAssembly, typeof(T));
            return result;
        }
    }
}
